package com.example.core.common.web;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 分页信息
 *
 * @author hedongzhou
 * @date 2018/06/21
 */
public class PageInfo {

    /**
     * 当前页码
     */
    @JsonProperty("page")
    protected Integer page;

    /**
     * 当前页大小
     */
    @JsonProperty("limit")
    protected Integer limit;

    /**
     * 当前搜索条件下总记录数量
     */
    @JsonProperty("total")
    protected Integer total = 0;

    public PageInfo() {
        this.page = 1;
        this.limit = 20;
    }

    public PageInfo(Integer page, Integer limit) {
        this.page = page;
        this.limit = limit;
    }

    public PageInfo(Integer page, Integer limit, Integer defaultPage, Integer defaultLimit) {
        this.page = page != null ? page : defaultPage;
        this.limit = limit != null ? limit : defaultLimit;
    }

    public PageInfo(Integer page, Integer limit, Integer defaultLimit) {
        this(page, limit, 1, defaultLimit);
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public PageInfo default20LimitInvoke() {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 20;
        }
        return new PageInfo(page, limit);
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "page=" + page +
                ", limit=" + limit +
                ", total=" + total +
                '}';
    }

}
